<?php


namespace api\controllers;


use api\components\BaseController;
use common\helpers\commonApi;
use common\helpers\PageApi;
use common\helpers\TransformApi;
use common\models\Equity;
use common\models\EquityClassify;
use Yii;

/**
 * 权益
 * Class EquityController
 * @package api\controllers
 */
class EquityController extends BaseController
{
    /**
     * 分类
     * @return array
     */
    public function actionClassify()
    {
        $data = EquityClassify::find()
            ->where(['status'=>1])
            ->orderBy(['sort'=>SORT_DESC,'create_time'=>SORT_DESC])
            ->select(['name','icon','id as c_id'])
            ->asArray()->all();
        foreach ($data as &$datum){
            $datum['icon'] = commonApi::getDomain($datum['icon']);
        }
        return commonApi::message('成功',true,$data);
    }

    /**
     * 精品
     * @return array
     */
    public function actionBoutique()
    {
        $get = Yii::$app->request->get();
        $page = (int)$get['page']??1;
        $long = $get['long']??'';
        $lat = $get['lat']??'';
        if ($long||$lat){
            if (!is_numeric($long)||!is_numeric($lat)){
                return commonApi::message('位置信息错误！',false);
            }
        }
        return (new Equity())->getList(['is_boutique'=>1,'status'=>1],$page,$long,$lat);
    }

    /**
     * 列表
     * @return array
     */
    public function actionList()
    {
        $get = Yii::$app->request->get();
        $long = $get['long']??'';
        $lat = $get['lat']??'';
        $c_id = (int)$get['c_id']??'';
        $page = (int)$get['page']??1;
        if ($long||$lat){
            if (!is_numeric($long)||!is_numeric($lat)){
                return commonApi::message('位置信息错误！',false);
            }
        }
        return (new Equity())->getList(['c_id'=>$c_id,'status'=>1],$page,$long,$lat);
    }

    /**
     * 详情
     * @return array
     */
    public function actionDetails()
    {
        $get = Yii::$app->request->get();
        $e_id = (int)$get['e_id']??'';
        if (!$e_id){
            return commonApi::message('请选择要查看的！',false);
        }
        $model = Equity::find()
            ->where(['id'=>$e_id,'status'=>1])
            ->select(['img','desc','name','logo','address','mobile','long','lat'])
            ->one();
        if (!$model){
            return commonApi::message('数据错误！',false);
        }
        $model->views += 1;
        $model->save();
        $model->img = commonApi::getDomain($model->img);
        $model->logo = commonApi::getDomain($model->logo);
        $data = $model->toArray();
        return commonApi::message('成功',true,$data);
    }
}